package org.ki4.kipastelsys

import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils;
import org.ki4.kipastelsys.auth.*
import grails.plugins.springsecurity.Secured;

class FuncionarioController {
	def springSecurityService

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	@Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def index = {
		if (SpringSecurityUtils.ifAllGranted('ROLE_ADMIN'))
			redirect(action: "index", controller: "admin")
		request.funcionario = getCurrentFuncionario()
	}

	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[funcionarioInstanceList: Funcionario.list(params), funcionarioInstanceTotal: Funcionario.count()]
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def create = {
		def funcionarioInstance = new Funcionario()
		funcionarioInstance.properties = params
		funcionarioInstance.setEnabled true
		return [funcionarioInstance: funcionarioInstance]
	}

	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def save = {
		def funcionarioInstance = new Funcionario(params)
		funcionarioInstance.setPassword springSecurityService.encodePassword(funcionarioInstance.getPassword())
		if (funcionarioInstance.save(flush: true)) {
			PessoaFuncao.create funcionarioInstance, Funcao.findByAuthority("ROLE_FUNCIONARIO"), true
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), funcionarioInstance.id])}"
			redirect(action: "show", id: funcionarioInstance.id)
		}
		else {
			render(view: "create", model: [funcionarioInstance: funcionarioInstance])
		}
	}

	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def show = {
		def funcionarioInstance = Funcionario.get(params.id)
		if (!funcionarioInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), params.id])}"
			redirect(action: "list")
		}
		else {
			[funcionarioInstance: funcionarioInstance]
		}
	}

	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def edit = {
		def funcionarioInstance = Funcionario.get(params.id)
		if (!funcionarioInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [funcionarioInstance: funcionarioInstance]
		}
	}

	@Secured(['ROLE_FUNCIONARIO', 'IS_AUTHENTICATED_FULLY'])
	def editSenha = {
		def funcionarioInstance = getCurrentFuncionario()
		if (!funcionarioInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), params.id])}"
			redirect(action: "index")
		}
		else {
			return [funcionarioInstance: funcionarioInstance]
		}
	}

	@Secured(['ROLE_ADMIN', 'ROLE_FUNCIONARIO', 'IS_AUTHENTICATED_FULLY'])
	def update = {
		def funcionarioInstance = Funcionario.get(params.id)
		if (funcionarioInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (funcionarioInstance.version > version) {
					funcionarioInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'funcionario.label', default: 'Funcionario')]
					as Object[], "Another user has updated this Funcionario while you were editing")
					if (SpringSecurityUtils.ifAllGranted('ROLE_ADMIN'))
						render(view: "edit", model: [funcionarioInstance: funcionarioInstance])
					else
						render(view: "editSenha", model: [funcionarioInstance: funcionarioInstance])
					return
				}
			}
			funcionarioInstance.properties = params
			funcionarioInstance.setPassword springSecurityService.encodePassword(funcionarioInstance.getPassword())
			
			if (!funcionarioInstance.hasErrors() && funcionarioInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'funcionario.label', default: 'Senha'), ''])}"
				if (SpringSecurityUtils.ifAllGranted('ROLE_ADMIN'))
					redirect(action: "show", id: funcionarioInstance.id)
				else
					render(view: "editSenha", model: [funcionarioInstance: funcionarioInstance])
			}
			else {
				if (SpringSecurityUtils.ifAllGranted('ROLE_ADMIN'))
					render(view: "edit", model: [funcionarioInstance: funcionarioInstance])
				else
					render(view: "editSenha", model: [funcionarioInstance: funcionarioInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), params.id])}"
			if (SpringSecurityUtils.ifAllGranted('ROLE_ADMIN'))
				redirect(action: "list")
			else
				render(view: "editSenha", model: [funcionarioInstance: funcionarioInstance])
		}
	}

	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def delete = {
		def funcionarioInstance = Funcionario.get(params.id)
		if (funcionarioInstance) {
			try {
				funcionarioInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'funcionario.label', default: 'Funcionario'), params.id])}"
			redirect(action: "list")
		}
	}

	private getCurrentFuncionario() {
		Funcionario.get(springSecurityService.principal.id)
	}
}
